package com.winning.ds.login.service.impl;


import com.winning.ds.common.entity.CommonJgUserEntity;
import com.winning.ds.common.entity.SysGncdEntity;
import com.winning.ds.common.entity.SysUserEntity;
import com.winning.ds.login.dao.IndexDao;
import com.winning.ds.login.service.IndexService;
import com.winning.ds.login.entity.DbsxEntity;
import com.winning.ds.role.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: plat
 * @description: 首页菜单
 * @author: nxs
 * @create: 2018-07-20 15:25
 **/
@Service("IndexServiceImpl")
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexDao indexDao;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private IndexService indexService;

    @Value("${spring.application.yydm:DQMT}")
    private String YYDM;

    @Override
    public List<SysGncdEntity> queryNavigationMenus(List ryjsCollect) {
        return indexDao.queryNavigationMenus(YYDM, ryjsCollect);
    }

    @Override
    public List<SysGncdEntity> queryNavigationMenu(List ryjsCollect) {
        return indexDao.queryNavigationMenu(ryjsCollect);
    }

    @Override
    public List<SysGncdEntity> queryMenu(String  mcode) {
        return indexDao.queryMenu(mcode);
    }

    @Override
    public List<String> getJsbmList() {
        try {
            CommonJgUserEntity principal = (CommonJgUserEntity) SecurityUtils.getSubject().getPrincipal();
            //String rybm = principal.getRybm();
            String zclsh = principal.getZclsh();
            List<SysUserEntity> userRole = sysUserService.getUserRole(zclsh);
            return userRole.stream().map(x -> x.getJsbm()).collect(Collectors.toList());
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }


    @Override
    public Map<String, Object> querySubMenu(String mcode) {

        Map<String, Object> resultMap = new HashMap<>();

        List<SysGncdEntity> secondMenu = indexDao.querySubMenu(mcode, indexService.getJsbmList());

        List<SysGncdEntity> secondList = new ArrayList<>();
        if (secondMenu.size() > 0) {

            for (SysGncdEntity sge : secondMenu) {
                List list = new ArrayList<>();
                SysGncdEntity sysge = new SysGncdEntity();
                sysge = sge;
                List<SysGncdEntity> thirdMenu = indexDao.querySubMenu(sge.getMcode(), indexService.getJsbmList());
                if (thirdMenu.size() > 0) {
                    sysge.setSubFlag("1");
                    sysge.setThirdlist(thirdMenu);
                } else {
                    sysge.setSubFlag("0");
                }
                list.add(sysge);
                secondList.addAll(list);
            }
        }
        resultMap.put("secondMenu", secondList);
        return resultMap;
    }

    @Override
    public Map<String, Object> queryXtglMenu(String pcode) {

        Map<String, Object> resultMap = new HashMap<>();

        List<SysGncdEntity> secondMenu = indexDao.querySubCd(pcode);
        List<SysGncdEntity> secondList = new ArrayList<>();

        for(SysGncdEntity sysGncdEntity:secondMenu){
            List list = new ArrayList<>();
            SysGncdEntity sysge = sysGncdEntity;
            String pcode1 = sysge.getMcode();
            List<SysGncdEntity> thirdMenu = indexDao.querySubCd(pcode1);
            if (thirdMenu.size() > 0) {
                sysge.setSubFlag("1");
                sysge.setThirdlist(thirdMenu);
            } else {
                sysge.setSubFlag("0");
            }
            list.add(sysge);
            secondList.addAll(list);
        }
        resultMap.put("secondMenu", secondList);
        return resultMap;
    }

  @Override
  public List<String> queryAllMenu(String dldm) {

    List<String> menus = indexDao.queryAllMenu(dldm);

    return menus;
  }


    //修改根机构编码
    @Override
    @Transactional
    public void updateRootJgbm(String jgbm) {
        //修改机构编码
        indexDao.updateJgbm(jgbm);
        indexDao.updateGldnJgbm(jgbm);
        indexDao.updateJgqx(jgbm);
        indexDao.updateCzjs(jgbm);
        indexDao.updateCzjsQx(jgbm);
        indexDao.updateCzry(jgbm);
        indexDao.updateCzryJs(jgbm);
    }

    @Override
    public void updateDefaultSheng(String csz) {
        indexDao.updateDefaultSheng(csz);
    }


    @Override
    public Integer queryJgTotalNum(Map<String, String> queryParams) {
        return indexDao.queryJgTotalNum(queryParams);
    }

    @Override
    public Integer queryYhryTotalNum(Map<String, String> queryParams) {
        return indexDao.queryYhryTotalNum(queryParams);
    }

    @Override
    public Integer queryTemplateNum(Map<String, String> queryParams) {
        return indexDao.queryTemplateNum(queryParams);
    }

    @Override
    public Integer queryMlMapping() {
        return indexDao.queryMlMapping();
    }

    @Override
    public Integer queryTermMapping() {
        return indexDao.queryTermMapping();
    }

    @Override
    public DbsxEntity dbsxList(Map<String, String> queryParams) {

        DbsxEntity dbsxEntity = new DbsxEntity();
        String lb=queryParams.get("lb");
        if(lb.equals("ypnum")){
            Integer dbsxListyp = indexDao.dbsxListyp(queryParams);
            dbsxEntity.setYpnum(String.valueOf(dbsxListyp));
        }else if(lb.equals("xmnum")){
            Integer dbsxListxm = indexDao.dbsxListxm(queryParams);
            dbsxEntity.setXmnum(String.valueOf(dbsxListxm));
        }else if(lb.equals("clnum")){
            Integer dbsxListcl = indexDao.dbsxListcl(queryParams);
            dbsxEntity.setClnum(String.valueOf(dbsxListcl));
        }else if(lb.equals("ssnum")){
            Integer dbsxListss = indexDao.dbsxListss(queryParams);
             dbsxEntity.setSsnum(String.valueOf(dbsxListss));
        }else if(lb.equals("zdnum")){
            Integer dbsxListzd = indexDao.dbsxListzd(queryParams);
            dbsxEntity.setZdnum(String.valueOf(dbsxListzd));
        }else if(lb.equals("ksnum")){
            Integer dbsxListks = indexDao.dbsxListks(queryParams);
            dbsxEntity.setKsnum(String.valueOf(dbsxListks));
        }
        return dbsxEntity;
    }

    @Override
    public void updateUser(SysUserEntity user){
        indexDao.updateUser(user);
    }

    @Override
    public Map<String, Object> queryCard() {
        List<Map<String,Object>> cards = indexDao.queryCard();
        return cards.get(0);
    }

    @Override
    public Map<String, String> querySjsjhx() {
        List<String> sjsjhx = indexDao.querySjsjhx();
        Map<String,String> result = new HashMap<>();
        result.put("wtjgxl",sjsjhx.get(0));
        result.put("jghl",sjsjhx.get(1));
        result.put("gxzq",sjsjhx.get(2));
        return result;
    }

    @Override
    public List<Map<String, Object>> queryDcrz() {
        return indexDao.queryDcrz();
    }

    @Override
    public List<SysGncdEntity> queryGncd(String zclsh, String mcode, String applicationId) {
        return indexDao.queryGncd(zclsh, mcode, applicationId);
    }

    /**
     *添加帮助文档
     */
    @Override
    public int addHelpHtml(Map<String, Object> queryParams){
        return indexDao.addHelpHtml(queryParams);
    }

    /**
     *查询帮助文档
     */
    @Override
    public List<Map<String,Object>> getHelpHtml(Map<String, Object> queryParams){
        return indexDao.getHelpHtml(queryParams);
    }

    /**
     *更新帮助文档
     */
    @Override
    public void updateHelpHtml(Map<String, Object> queryParams){
        indexDao.updateHelpHtml(queryParams);
    }

}
